<template>
    <el-drawer v-model="showDrawer" :title="title" :size="size" :destroy-on-close="destroyOnClose"
        :close-on-click-modal="false">
        <div class="formDrawer">
            <div class="body">
                <slot></slot>
            </div>

            <div class="actions">
                <el-button type="primary" @click="submit" :loading="loading">{{ confirmText }}</el-button>
                <el-button type="default" @click="close">取消</el-button>
            </div>
        </div>
    </el-drawer>
</template>

<script setup>
import { ref } from 'vue'

const showDrawer = ref(false)   // 控制抽屉的显示与隐藏，默认关闭

const porps = defineProps({
    title: String,  // 抽屉标题
    size: {
        type: String,
        default: '45%'  // 抽屉默认大小

    },
    destroyOnClose: {
        type: Boolean,
        default: false // 是否在关闭时销毁组件
    },
    confirmText: {
        type: String,
        default: '提交'
    }
})

const loading = ref(false)
const showLoading = () => loading.value = true
const hideLoading = () => loading.value = false

// 打开抽屉面板
const open = () => showDrawer.value = true

// 关闭抽屉面板
const close = () => showDrawer.value = false

// 提交
const emit = defineEmits(['submit'])
const submit = () => emit('submit')


// 像父组件暴露以下方法
defineExpose({
    open,
    close,
    showLoading,
    hideLoading
})
</script>

<style>
.formDrawer {
    width: 100%;
    height: 100%;
    position: relative;
    @apply flex flex-col;
}

.formDrawer .body {
    flex: 1;
    overflow-y: auto;
}

.formDrawer .actions {
    height: 50px;
    @apply mt-auto flex items-center;
}
</style>